/*
 * ZISZApp.java
 */
package wat.clientapp.main;

import java.security.InvalidKeyException;
import java.util.EventObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import wat.clientapp.connection.SecureConnection;
import wat.ziszcommon.invoker.UnauthorizedAccesException;
import wat.ziszcommon.remote.ClassProvider;
import wat.ziszcommon.remote.RemoteModuleFactory;

/**
 * The main class of the application.
 */
public class ZISZApp extends SingleFrameApplication {

   /**
    * At startup create and show the main frame of the application.
    */
   @Override
   protected void startup() {
      // show(new ZISZView(this));
      this.addExitListener(new MyExitListener());
      new LoginForm(this).setVisible(true);
   }

   /**
    * This method is to initialize the specified window by injecting resources.
    * Windows shown in our application come fully initialized from the GUI
    * builder, so this additional configuration is not needed.
    */
   @Override
   protected void configureWindow(java.awt.Window root) {
   }

   /**
    * A convenient static getter for the application instance.
    * @return the instance of ZISZApp
    */
   public static ZISZApp getApplication() {
      return Application.getInstance(ZISZApp.class);
   }

   /**
    * Main method launching the application.
    */
   public static void main(String[] args) {
      launch(ZISZApp.class, args);
   }

   /*
    * Klasa pilnujaca, by przy zamykaniu dopilnowac wszelkich formalnosci, tj.:
    *  - wylogowac sie (zamknac sesje) z serwera
    *  - ... tbd.
    */
   private class MyExitListener implements ExitListener {

      public boolean canExit(EventObject arg0) {
         /*try {
            // sprawdzenie polaczenia przy zamykanie aplikacji
            SecureConnection.getInstance().keepAlive();
         } catch (InvalidKeyException ex) {  // nie powinno sie nigdy zdarzyc
            Logger.getLogger(ZISZApp.class.getName()).log(Level.SEVERE, "jakim cudem?", ex);
         } catch (UnauthorizedAccesException ex) {
            Logger.getLogger(ZISZApp.class.getName()).log(Level.SEVERE, "blad w polaczeniu z serwerem", ex);
            // ew dodac info dla uzytkownika z pytaniem, czy faktycznie zamykac
         }*/
         return true;
      }

      public void willExit(EventObject arg0) {
         try {
            SecureConnection.getInstance().logout();
            Logger.getLogger(ZISZApp.class.getName()).log(Level.INFO, "zamykanie aplikacji", arg0);
         } catch (InvalidKeyException ex) {  // nie powinno sie nigdy zdarzyc
            Logger.getLogger(ZISZApp.class.getName()).log(Level.SEVERE, "jakim cudem?", ex);
         } catch (UnauthorizedAccesException ex) {
            Logger.getLogger(ZISZApp.class.getName()).log(Level.SEVERE, "blad przy wylogowywaniu", ex);
         }
      }
   }
}
